package com.ssm.service.impl;

import com.alibaba.dubbo.config.annotation.Reference;
import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.ssm.base.BaseDao;
import com.ssm.base.BaseServiceImpl;
import com.ssm.dao.UserFollowDao;
import com.ssm.entity.UserFollow;
import com.ssm.service.DictService;
import com.ssm.service.UserFollowService;
import com.ssm.vo.UserFollowVo;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.List;

/**
 * @Author jie_ge
 * @Date 2022/4/21
 */
@Service(interfaceClass = UserFollowService.class)
public class UserFollowServiceImpl extends BaseServiceImpl<UserFollow> implements UserFollowService {
    @Autowired
    private UserFollowDao userFollowDao;
    @Reference
    private DictService dictService;
    @Override
    protected BaseDao<UserFollow> getEntityDao() {
        return userFollowDao;
    }


    /**
     * 关注房源
     * @param userId
     * @param houseId
     */
    @Override
    public void follow(Long userId, Long houseId) {
        UserFollow userFollow = new UserFollow();
        userFollow.setUserId(userId);
        userFollow.setHouseId(houseId);
        userFollowDao.insert(userFollow);
    }

    /**
     * 是否关注房源
     * @param userId
     * @param houseId
     * @return
     */
    @Override
    public Boolean isFollow(Long userId, Long houseId) {
        Integer count = userFollowDao.countByUserIdAndHouseId(userId, houseId);
        //intValue()方法是一个非静态方法，只能通过类对象访问，如果尝试使用类名称访问该方法，则会收到错误消息。
        if(count.intValue() == 0){
            return false;
        }
        return true;
    }

    /**
     * 我的关注
     * @param pageNum
     * @param pageSize
     * @param userId
     * @return
     */
    @Override
    public PageInfo<UserFollowVo> findListPage(int pageNum, int pageSize, Long userId) {
        //开启分页
        PageHelper.startPage(pageNum, pageSize);
        //查询分页数据
        Page<UserFollowVo> page = userFollowDao.findListPage(userId);
        //获取分页集合
        List<UserFollowVo> list = page.getResult();
        for (UserFollowVo userFollowVo : list) {
            //户型
            String houseTypeName = dictService.getNameById(userFollowVo.getHouseTypeId());
            //楼层
            String floorName = dictService.getNameById(userFollowVo.getFloorId());
            //朝向
            String directionName = dictService.getNameById(userFollowVo.getDirectionId());

            userFollowVo.setHouseTypeName(houseTypeName);
            userFollowVo.setFloorName(floorName);
            userFollowVo.setDirectionName(directionName);
        }
        return new PageInfo<UserFollowVo>(page,10);
    }

    /**
     * 取消房源
     * @param id
     */
    @Override
    public void cancelFollow(Long id) {
        userFollowDao.deleteById(id);
    }
}
